package team.damo.common.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import team.damo.common.enums.ResponseCode;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: ZhangHeng
 * @Description: TODO
 * @DateTime: 2024/2/26 21:21
 **/

@Data
@AllArgsConstructor
public class ResponseEntity implements CustomizeResponseCode {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty (value= "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data=new HashMap<>();
    /*
    构造方法私有化，里面的方法都为静态方法
    达到保护属性的作用
     */
    private ResponseEntity(){

    }
    /*
    使用链式编程
     */
    public static ResponseEntity ok(){
        ResponseEntity result = new ResponseEntity();
        result.setSuccess(true);
        result.setCode(ResponseCode.SUCCESS.getCode());
        result.setMessage(ResponseCode.SUCCESS.getMessage());
        return result;
    }

    public static ResponseEntity error(){
        ResponseEntity result=new ResponseEntity();
        result.setSuccess(false);
        result.setCode(ResponseCode.ERROR.getCode());
        result.setMessage(ResponseCode.ERROR.getMessage());
        return result;
    }
    /*
    自定义返回成功与否
     */
    public ResponseEntity success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    public ResponseEntity message(String message){
        this.setMessage(message);
        return this;
    }
    public ResponseEntity code(Integer code){
        this.setCode(code);
        return this;
    }
    public ResponseEntity data(String key, Object value){
        this.data.put(key,value);
        return this;
    }
    public ResponseEntity data(Map<String,Object>map){
        this.setData(map);
        return this;
    }
    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }
}
